package com.education.wisdom.controller.app;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.Catalog;
import com.education.wisdom.pojo.Resources;
import com.education.wisdom.service.ICatalogService;
import com.education.wisdom.service.IResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 扫描二维码显示课程详情
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@Api(tags = {"专业课程体系目录"})
@RequestMapping("/qrcode/catalog")
public class QrcodeCatalogController {


    @Autowired
    private ICatalogService catalogService;
    @Autowired
    private IResourcesService resourcesService;



    @GetMapping("/info")
    @ApiOperation("查询课程详情")
    public void classInfo(Integer catalogId, HttpServletRequest request, HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询课程详情", new Date()));

//        String token = JwtTokenUtil.getToken(request);
//        String username = JwtTokenUtil.getUsernameFromToken(token);

        Catalog catalog = catalogService.classInfo(catalogId);

        if (catalog.getCatalogLearningNum() == null) {
            catalog.setCatalogLearningNum(1);
        } else {
            catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
        }
        catalogService.updateCataLog(catalog);
        Catalog catalog1 = catalogService.selectCatalogInfoById(catalog.getParentId());
        catalog.setTwoJi(catalog1.getName());

        Catalog catalog2 = catalogService.selectCatalogInfoById(catalog1.getParentId());
        if (catalog2 != null) {
            catalog.setOneJi(catalog2.getName());
        }
        writeJSON(catalog, response, "200", "success");

    }


    /**
     * 虚拟教学
     * @param response
     * @param catalogId
     * @param pageNum
     * @param pageSize
     * @throws IOException
     */
    @GetMapping("/virtualTeaching")
    public void virtualTeaching(HttpServletResponse response, Integer catalogId, Integer pageNum, Integer pageSize) throws
            IOException {
        PageHelper.startPage(pageNum, pageSize);
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog.setThreeD(resource.getrUrl());
                }
            }
        }
        writeJSON(new PageInfo<>(catalogs), response, "200", "success");
    }

    @GetMapping("/type")
    @ApiOperation("查询教学课件信息")
    public void selectByType(HttpServletResponse response, String catalogId, String type, Integer pageNum, Integer pageSize) throws IOException {
        log.info(LogUtil.LogInfo("查询素材课件信息", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("rCatalogid", catalogId);
        map.put("type", type);
        PageHelper.startPage(pageNum, pageSize);
        List<Resources> list = resourcesService.selectInfoByType(map);
        PageInfo<Resources> page = new PageInfo<>(list);
        writeJSON(page, response, "200", "success");
    }
}


